package com.ruoyi.standingbook.park.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.standingbook.park.domain.bo.ParkImportDetailBo;
import com.ruoyi.standingbook.park.domain.vo.ParkImportDetailVo;
import com.ruoyi.standingbook.park.domain.ParkImportDetail;
import com.ruoyi.standingbook.park.mapper.ParkImportDetailMapper;
import com.ruoyi.standingbook.park.service.IParkImportDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 公园管养导入明细Service业务层处理
 *
 * @author cc
 * @date 2023-08-29
 */
@RequiredArgsConstructor
@Service
public class ParkImportDetailServiceImpl implements IParkImportDetailService {

    private final ParkImportDetailMapper baseMapper;

    /**
     * 查询公园管养导入明细
     */
    @Override
    public ParkImportDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询公园管养导入明细列表
     */
    @Override
    public TableDataInfo<ParkImportDetailVo> queryPageList(ParkImportDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ParkImportDetail> lqw = buildQueryWrapper(bo);
        Page<ParkImportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询公园管养导入明细列表
     */
    @Override
    public List<ParkImportDetailVo> queryList(ParkImportDetailBo bo) {
        LambdaQueryWrapper<ParkImportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ParkImportDetail> buildQueryWrapper(ParkImportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ParkImportDetail> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), ParkImportDetail::getItemName, bo.getItemName());
        lqw.eq(bo.getServiceType()!=null, ParkImportDetail::getServiceType, bo.getServiceType());
        lqw.like(StringUtils.isNotBlank(bo.getParkName()), ParkImportDetail::getParkName, bo.getParkName());
        lqw.eq(StringUtils.isNotBlank(bo.getCommunity()), ParkImportDetail::getCommunity, bo.getCommunity());
        lqw.eq(StringUtils.isNotBlank(bo.getLocation()), ParkImportDetail::getLocation, bo.getLocation());
        lqw.eq(bo.getGreenArea() != null, ParkImportDetail::getGreenArea, bo.getGreenArea());
        lqw.eq(bo.getWoodlandArea() != null, ParkImportDetail::getWoodlandArea, bo.getWoodlandArea());
        lqw.eq(bo.getWatershedArea() != null, ParkImportDetail::getWatershedArea, bo.getWatershedArea());
        lqw.eq(StringUtils.isNotBlank(bo.getGreenLevel()), ParkImportDetail::getGreenLevel, bo.getGreenLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getWoodlandLevel()), ParkImportDetail::getWoodlandLevel, bo.getWoodlandLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getWatershedLevel()), ParkImportDetail::getWatershedLevel, bo.getWatershedLevel());
        lqw.eq(bo.getDate() != null, ParkImportDetail::getDate, bo.getDate());
        lqw.eq(bo.getFrequency() != null, ParkImportDetail::getFrequency, bo.getFrequency());
        lqw.eq(bo.getActualFrequency() != null, ParkImportDetail::getActualFrequency, bo.getActualFrequency());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ParkImportDetail::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPic1()), ParkImportDetail::getPic1, bo.getPic1());
        lqw.eq(StringUtils.isNotBlank(bo.getPic2()), ParkImportDetail::getPic2, bo.getPic2());
        lqw.eq(StringUtils.isNotBlank(bo.getPic3()), ParkImportDetail::getPic3, bo.getPic3());
        lqw.eq(StringUtils.isNotBlank(bo.getPic4()), ParkImportDetail::getPic4, bo.getPic4());
        lqw.eq(StringUtils.isNotBlank(bo.getProviderId()), ParkImportDetail::getProviderId, bo.getProviderId());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), ParkImportDetail::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getProcessStatus() != null, ParkImportDetail::getProcessStatus, bo.getProcessStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getProcessMsg()), ParkImportDetail::getProcessMsg, bo.getProcessMsg());
        return lqw;
    }

    /**
     * 新增公园管养导入明细
     */
    @Override
    public Boolean insertByBo(ParkImportDetailBo bo) {
        ParkImportDetail add = BeanUtil.toBean(bo, ParkImportDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改公园管养导入明细
     */
    @Override
    public Boolean updateByBo(ParkImportDetailBo bo) {
        ParkImportDetail update = BeanUtil.toBean(bo, ParkImportDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ParkImportDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除公园管养导入明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean batchInsertVo(List<ParkImportDetailVo> voList) {
        List<ParkImportDetail> list= BeanCopyUtils.copyList(voList , ParkImportDetail.class);
        return baseMapper.insertBatch(list);
    }
}
